﻿using System;
using System.Windows.Forms;

namespace RibbonEditor.Controls
{
    public partial class IntegerControl : UserControl, IDirtable
    {
        bool firstAccess = true;

        bool isOnlyPositive;
        bool isOnlyNegative;

        int? initialValue = null;

        public bool IsDirty
        {

            set { if (value == false && this.txtText.Text.Length > 0) this.initialValue = int.Parse(this.txtText.Text); this.isDirty = value; }
            get { return this.isDirty; }
        }

        private bool isDirty;

        public string Label
        {
            get
            {
                return this.lblLabel.Text;
            }
            set
            {
                this.lblLabel.Text = value;
            }
        }

        public int? Value
        {
            get
            {
                if (this.txtText.Text.Length > 0)
                {
                    return int.Parse(this.txtText.Text);
                }
                else
                {
                    return null;
                }
            }
            set
            {
                if (!firstAccess)
                {
                    if (this.initialValue == null)
                        this.initialValue = value;
                }
                else
                {
                    firstAccess = false;
                }

                this.txtText.Text = value.ToString();
            }
        }

        public bool IsOnlyNegative
        {
            set
            {
                this.isOnlyNegative = value;

                if (value == true)
                {
                    this.IsOnlyPositive = false;
                }
            }
            get
            {
                return this.isOnlyNegative;
            }
        }

        public bool IsOnlyPositive
        {
            set
            {
                this.isOnlyPositive = value;

                if (value == true)
                {
                    this.IsOnlyNegative = false;
                }
            }
            get
            {
                return this.isOnlyPositive;
            }
        }

        public bool IsRequired
        {
            get
            {
                return this.lblRequired.Visible;
            }
            set
            {
                this.lblRequired.Visible = value;
            }
        }

        bool isEditable = true;

        public bool IsEditable
        {
            get { return isEditable; }
            set { isEditable = value; }
        }

        public IntegerControl()
        {
            //this.txtText.TextChanged -= new System.EventHandler(this.txtText_TextChanged);

            InitializeComponent();

            this.txtText.Enabled = isEditable;

            this.txtText.TextChanged += new System.EventHandler(this.txtText_TextChanged);
        }

        private void txtText_TextChanged(object sender, EventArgs e)
        {
            if (txtText.Text.Length > 0)
            {
                int parsedInteger;

                if (int.TryParse(this.txtText.Text, out parsedInteger))
                {
                    if (this.IsOnlyPositive && parsedInteger < 0)
                    {
                        MessageBox.Show(this, "Only positive integer value is supported", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtText.Text = string.Empty;
                    }
                    else if (this.IsOnlyNegative && parsedInteger > 0)
                    {
                        MessageBox.Show(this, "Only negative integer value is supported!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtText.Text = string.Empty;
                    }
                    else
                    {
                        this.Value = parsedInteger;
                        this.IsDirty = this.initialValue != int.Parse(this.txtText.Text);
                    }
                }
                else
                {
                    MessageBox.Show(this, "Only integer value is supported!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtText.Text = this.txtText.Text.Substring(0, this.txtText.Text.Length - 1);
                }
            }
        }

        public void ReinitValues()
        {
            this.txtText.Text= this.initialValue.Value.ToString();
            this.isDirty = false;
        }
    }
}
